package com.arrogames.rgblabyrinth {
	import com.arrogames.rgblabyrinth.controller.RGBController;
	import com.arrogames.rgblabyrinth.game.GameEngine;
	import com.arrogames.rgblabyrinth.game.ui.GamePanel;
	import com.arrogames.rgblabyrinth.menu.LoadApplicationScreen;
	import com.arrogames.rgblabyrinth.menu.RGBNavigator;
	import com.arrogames.rgblabyrinth.menu.components.OptionsMenu;
	import com.arrogames.rgblabyrinth.model.RGBModel;
	import com.arrogames.rgblabyrinth.model.RGBModelEvent;
	import com.arrogames.util.Observable;
	import com.arrogames.util.Observer;
	import com.arrogames.util.UIUtil;
	
	import flash.display.Stage;
	
	import feathers.controls.Screen;
	import feathers.controls.ScreenNavigatorItem;
	
	import starling.core.Starling;
	import starling.display.Sprite;
	
	public class RGBLabyrinth extends Sprite implements Observer {
		
		//conventional MVC pattern used here.
		private var model:RGBModel;
		private var viewNavigator:RGBNavigator;
		private var optionsMenu:OptionsMenu;
		private var controller:RGBController;
		
		//Entity system pattern here, doesn't fit with conventional MVC pattern.
		//Mixing it up here, using the GameEngine as a bridge.
		private var uiContainer:Sprite;
		private var gamePanel:GamePanel;

		public function RGBLabyrinth() {
			super();
			
			viewNavigator = new RGBNavigator();
			createUI();
			var engine:GameEngine = new GameEngine(gamePanel);
			model = new RGBModel(engine);
			addChild(viewNavigator);
			model.addObserver(this);
			
			controller = new RGBController(model, this);
		}
		
		public function updateAssetProgress(ratio:Number):void {
			var loadingScreen:LoadApplicationScreen = viewNavigator.activeScreen as LoadApplicationScreen;
			loadingScreen.setProgress(ratio);
		}
		
		public function loadingComplete():void {
			model.initialize();
			
			optionsMenu = new OptionsMenu();
			this.addChild(UIUtil.move(optionsMenu, 800-(2*(24+10)+30), 10));
			
		}
		
		public function showScreen(id:String):void {
			viewNavigator.showScreen(id);
		}
		
		public function update(obs:Observable, obj:Object):void {
			
			if (obj == RGBModelEvent.INITIALIZED) {
				viewNavigator.saveGames = model.getSaveGames();
				viewNavigator.showScreen(RGBNavigator.MAINMENU);
				
			} else if (obj == RGBModelEvent.LOADING_LEVEL) {
				viewNavigator.showScreen(RGBNavigator.LOADINGGAME);
			
			} else if (obj == RGBModelEvent.LEVEL_LOADED) {
				viewNavigator.showScreen(RGBNavigator.GAME);
				optionsMenu.updateState(OptionsMenu.GAME);
			
			} else if (obj == RGBModelEvent.STOPPED_GAME) {
				optionsMenu.updateState(OptionsMenu.MENU);
				viewNavigator.showScreen(RGBNavigator.MAINMENU);
				viewNavigator.saveGames = model.getSaveGames();
			
			} else if (obj == RGBModelEvent.MUSIC_CHANGED) {
				optionsMenu.musicButton.isSelected = model.isMusicEnabled;
				
			} else if (obj == RGBModelEvent.SFX_CHANGED) {
				optionsMenu.sfxButton.isSelected = model.isSfxEnabled; 
			
			} else if (obj == RGBModelEvent.REMOVED_GAME) {
				viewNavigator.saveGames = model.getSaveGames();
			}
		}
		
		private function createUI():void {
			var nativeStage:Stage = Starling.current.nativeStage;
			var nativeStageWidth:int = nativeStage.stageWidth;
			var nativeStageHeight:int = nativeStage.stageHeight;
			var gameScreen:Screen = new Screen();
			
			gamePanel = new GamePanel(nativeStageWidth, nativeStageHeight, 0x2e2f33);
			gameScreen.addChild(gamePanel);
			uiContainer = new Sprite();
			gameScreen.addChild(uiContainer);
			
			viewNavigator.addScreen(RGBNavigator.GAME, new ScreenNavigatorItem(gameScreen));
		}
	}
}